# -*- coding:utf-8 -*-

import urllib.request
from bs4 import BeautifulSoup
import glob
import jieba
import re
import threading
import hashlib
import urllib.parse
import time
import timedelta
import datetime
import requests
from common.my_time import MyTime
from common.attr_v3 import AttrV3
from database.db_handler import MysqlHander
import json
import os
from selenium import webdriver
from webdriver_base import WebdriverBase
from common.my_file import MyFile

'''
上交所监管数据
'''

'''
chromedriver = "D:\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(executable_path="D:\\chromedriver.exe")

file = open('./out/shangjiaosuo_stock.txt', 'w', encoding = 'utf-8')
'''


class ShStockData(WebdriverBase):
    def __init__(self):
        #self.v3 = AttrV3()
        self.file = open('./txt/' + os.path.split(__file__)[-1].split(".")[0] + ".txt", 'w', encoding = 'utf-8')
        WebdriverBase.__init__(self, './log/' + os.path.split(__file__)[-1].split(".")[0] + ".log")

    def __del__(self):
        self.file.close()
        pass

    def gupiao_insert(self, code, name):
        trip_code = code.strip()
        trip_name = name.strip()
        sql = "insert into entity_gupiao (Fcode, Fname, Fexchange_id, Fcreate_time, Fmodify_time) values "
        sql += "('" + trip_code + "','" + trip_name + "',1 , now(), now());"
        #print(sql)
        insert = 0
        try:
            self.db.execute_not_safe(sql)
            insert = 1
        except Exception as e:
            pass
        if 1 == insert:
            return
        update_sql = "update entity_gupiao set Fmodify_time=now(),Fexchange_id=1,Fname='" + trip_name + "' where Fcode='" + trip_code + "';"
        print(update_sql)
        try:
            self.db.execute_not_safe(update_sql)
        except Exception as e:
            pass

    def query_page_data(self, page):
        trs = self.driver.find_element_by_tag_name("tbody").find_elements_by_tag_name("tr")
        for tr in trs:
            tds = tr.find_elements_by_tag_name("td")
            data_code = tds[2].text
            data_name = tds[3].text
            self.gupiao_insert(data_code, data_name)
            self.file.write(data_code + "|" + data_name + "|\n")
        
        next_page = self.driver.find_element_by_class_name("next")
        if next_page:
            next_page.click()
            return True
        else:
            return False
        
    def query_data(self):
        self.driver.get("http://www.sse.com.cn/assortment/stock/list/share/")
        time.sleep(10)
        page = 1
        while self.query_page_data(page):
            print(page)
            page = page + 1
            if page > 1700:
                break
            time.sleep(2)
        self.driver.quit()
    
    def get_data(self):
        self.query_data()

def shanghai_stock_api():
    d = ShStockData()
    d.get_data()

if __name__ == '__main__':
    d = ShStockData()
    d.get_data()

